package com.rickpan.dto.contacts;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.rickpan.entity.FriendRelationship;
import com.rickpan.entity.User;
import com.rickpan.entity.UserOnlineStatus;

import java.time.LocalDateTime;

/**
 * 好友信息响应DTO
 */
public class FriendResponse {

    private Long id;
    private Long userId;
    private String username;
    private String realName;
    private String avatarUrl;
    private User.UserType userType;
    private String groupName;
    private String remark;
    private UserOnlineStatus.Status onlineStatus;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastSeen;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime friendSince;

    private Long relationshipId;

    // 构造函数
    public FriendResponse() {}

    public FriendResponse(FriendRelationship relationship, User friend) {
        this.relationshipId = relationship.getId();
        this.userId = friend.getId();
        this.username = friend.getUsername();
        this.realName = friend.getRealName();
        this.avatarUrl = friend.getAvatarUrl();
        this.userType = friend.getUserType();
        this.groupName = relationship.getGroupName();
        this.remark = relationship.getRemark();
        this.friendSince = relationship.getCreatedAt();
        // 默认离线状态
        this.onlineStatus = UserOnlineStatus.Status.OFFLINE;
        this.lastSeen = LocalDateTime.now();
    }

    public FriendResponse(FriendRelationship relationship, User friend, UserOnlineStatus onlineStatus) {
        this(relationship, friend);
        if (onlineStatus != null) {
            this.onlineStatus = onlineStatus.getStatus();
            this.lastSeen = onlineStatus.getLastSeen();
        }
    }

    // 静态工厂方法
    public static FriendResponse from(FriendRelationship relationship, User friend) {
        return new FriendResponse(relationship, friend);
    }

    public static FriendResponse from(FriendRelationship relationship, User friend, UserOnlineStatus onlineStatus) {
        return new FriendResponse(relationship, friend, onlineStatus);
    }

    // Getter和Setter方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public User.UserType getUserType() {
        return userType;
    }

    public void setUserType(User.UserType userType) {
        this.userType = userType;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public UserOnlineStatus.Status getOnlineStatus() {
        return onlineStatus;
    }

    public void setOnlineStatus(UserOnlineStatus.Status onlineStatus) {
        this.onlineStatus = onlineStatus;
    }

    public LocalDateTime getLastSeen() {
        return lastSeen;
    }

    public void setLastSeen(LocalDateTime lastSeen) {
        this.lastSeen = lastSeen;
    }

    public LocalDateTime getFriendSince() {
        return friendSince;
    }

    public void setFriendSince(LocalDateTime friendSince) {
        this.friendSince = friendSince;
    }

    public Long getRelationshipId() {
        return relationshipId;
    }

    public void setRelationshipId(Long relationshipId) {
        this.relationshipId = relationshipId;
    }

    @Override
    public String toString() {
        return "FriendResponse{" +
                "id=" + id +
                ", userId=" + userId +
                ", username='" + username + '\'' +
                ", realName='" + realName + '\'' +
                ", avatarUrl='" + avatarUrl + '\'' +
                ", userType=" + userType +
                ", groupName='" + groupName + '\'' +
                ", remark='" + remark + '\'' +
                ", onlineStatus=" + onlineStatus +
                ", lastSeen=" + lastSeen +
                ", friendSince=" + friendSince +
                ", relationshipId=" + relationshipId +
                '}';
    }
}
